home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.4 / AppShell / Examples / Support / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  8.8 KB  |  366 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                            Preliminary                               *
  4.  *                        Amiga AppShell (tm)                           *
  5.  *                                                                      *
  6.  *  Copyright (c) 1990,1991 Commodore-Amiga, Inc. All Rights Reserved.  *
  7.  *                                                                      *
  8.  *   This software and information is proprietary, preliminary, and     *
  9.  *   subject to change without notice.                                  *
  10.  *                                                                      *
  11.  *                            DISCLAIMER                                *
  12.  *                                                                      *
  13.  *   THIS SOFTWARE IS PROVIDED "AS IS".                                 *
  14.  *   NO REPRESENTATIONS OR WARRANTIES ARE MADE WITH RESPECT TO THE      *
  15.  *   ACCURACY, RELIABILITY, PERFORMANCE, CURRENTNESS, OR OPERATION      *
  16.  *   OF THIS SOFTWARE, AND ALL USE IS AT YOUR OWN RISK.                 *
  17.  *   NEITHER COMMODORE NOR THE AUTHORS ASSUME ANY RESPONSIBILITY OR     *
  18.  *   LIABILITY WHATSOEVER WITH RESPECT TO YOUR USE OF THIS SOFTWARE.    *
  19.  *                                                                      *
  20.  *                          Non-Disclosure                              *
  21.  *                                                                      *
  22.  *   This information is not to be disclosed to any other company,      *
  23.  *   individual or party.  Discussion is to be restricted to CBM        *
  24.  *   approved discussion areas, such as the closed conferences on bix;  *
  25.  *   amiga.cert, amiga.com, amiga.beta/appshell.                        *
  26.  *                                                                      *
  27.  ************************************************************************
  28.  * Copyright (C) 1990 Commodore-Amiga, Inc.
  29.  * written by David N. Junod
  30.  */
  31.  
  32. #include <exec/types.h>
  33. #include <exec/lists.h>
  34. #include <exec/nodes.h>
  35. #include <exec/memory.h>
  36. #include <utility/hooks.h>
  37. #include <dos/dos.h>
  38. #include <dos/dosextens.h>
  39. #include <dos/dosasl.h>
  40. #include <libraries/asl.h>
  41. #include <libraries/appshell.h>
  42. #include <workbench/startup.h>
  43. #include <clib/alib_protos.h>
  44. #include <clib/exec_protos.h>
  45. #include <clib/dos_protos.h>
  46. #include <clib/intuition_protos.h>
  47. #include <clib/graphics_protos.h>
  48. #include <clib/appshell_protos.h>
  49. #include <clib/appobjects_protos.h>
  50. #include <clib/utility_protos.h>
  51. #include <pragmas/exec.h>
  52. #include <pragmas/dos.h>
  53. #include <pragmas/appshell.h>
  54. #include <pragmas/appobjects.h>
  55. #include <pragmas/utility.h>
  56. #include <string.h>
  57.  
  58. extern struct Library *SysBase, *AppObjectsBase, *UtilityBase, *DOSBase;
  59. extern struct WBStartup *WBenchMsg;
  60. struct Library *AppShellBase;
  61.  
  62. #if 1
  63. #include "main.c"
  64. #else
  65. /* Main processing loop */
  66. VOID main (int argc, char **argv)
  67. {
  68.     extern struct TagItem Our_App[];
  69.  
  70.     /* open the AppShell library */
  71.     if (AppShellBase = OpenLibrary ("appshell.library", 36))
  72.     {
  73.     /* Main AppShell entry point */
  74.     HandleApp (argc, argv, WBenchMsg, Our_App);
  75.  
  76.     /* close the AppShell library */
  77.     CloseLibrary (AppShellBase);
  78.     }
  79. }
  80. #endif
  81.  
  82. ULONG AddObj (struct ObjectInfo * oi, struct Window * win, ULONG data,...)
  83. {
  84.     return (AddObjList (oi, win, (struct TagItem *) & data));
  85. }
  86.  
  87. ULONG DelObj (struct ObjectInfo * oi, struct Window * win, ULONG data,...)
  88. {
  89.     return (RemoveObjList (oi, win, (struct TagItem *) & data));
  90. }
  91.  
  92. /* handle messages between function handlers */
  93. BOOL HandlerFunc (struct AppInfo * ai, ULONG tags,...)
  94. {
  95.     return (HandlerFuncA (ai, (struct TagItem *) & tags));
  96. }
  97.  
  98. /* get handler data */
  99. APTR HandlerData (struct AppInfo * ai, ULONG tags,...)
  100. {
  101.     return (HandlerDataA (ai, (struct TagItem *) & tags));
  102. }
  103.  
  104. VOID callfunc (VOID (*func) (), struct AppInfo * ai, STRPTR cmd, ULONG data,...)
  105. {
  106.     (*(func)) (ai, cmd, (struct TagItem *) & data);
  107. }
  108.  
  109. struct Node *NPrintf (struct List *l, STRPTR buffer, LONG size, VOID *fmt, VOID *data, ...)
  110. {
  111.     struct Node *n = NULL;
  112.     STRPTR buff = buffer;
  113.     LONG msize;
  114.  
  115.     /* Do we have a buffer to work with? */
  116.     if (!buff)
  117.     {
  118.     /* Allocate our own buffer */
  119.     buff = (STRPTR) AllocVec (512L, MEMF_CLEAR);
  120.     }
  121.  
  122.     /* Make sure we have a buffer to copy to */
  123.     if (buff)
  124.     {
  125.     /* Build the string */
  126.     sprintf (buff, fmt, data);
  127.  
  128.     /* How much room do we need */
  129.     msize = sizeof (struct Node) + strlen (buff) + 1L;
  130.  
  131.     /* Allocate a node to hold the tool type */
  132.     if (n = (struct Node *) AllocVec (msize, MEMF_CLEAR))
  133.     {
  134.         /* Copy the string into the node */
  135.         n->ln_Name = MEMORY_FOLLOWING (n);
  136.         strcpy (n->ln_Name, buff);
  137.  
  138.         /* Add the node to the end of the list */
  139.         AddTail (l, n);
  140.     }
  141.     }
  142.  
  143.     /* Did we allocate the buffer? */
  144.     if (buff && (buff != buffer))
  145.     {
  146.     /* Free the buffer */
  147.     FreeVec (buff);
  148.     }
  149.  
  150.     return (n);
  151. }
  152.  
  153. struct Node *MakeNode (struct List * list, STRPTR name)
  154. {
  155.     struct Node *node = NULL;
  156.  
  157.     if (list && name)
  158.     {
  159.     LONG msize;
  160.  
  161.     /* Compute the size of the memory allocation */
  162.     msize = sizeof (struct Node) + strlen (name) + 2L;
  163.  
  164.     /* Allocate the node */
  165.     if (node = (struct Node *) AllocVec (msize, MEMF_CLEAR))
  166.     {
  167.         node->ln_Name = MEMORY_FOLLOWING (node);
  168.  
  169.         /* Copy the name over */
  170.         strcpy (node->ln_Name, name);
  171.  
  172.         /* Add the node to the list */
  173.         AddTail (list, node);
  174.     }
  175.     }
  176.  
  177.     return (node);
  178. }
  179.  
  180. LONG MakeNodes (struct List * list, STRPTR * text, LONG st, LONG num)
  181. {
  182.     LONG cnt = 0L;
  183.  
  184.     if (list && text)
  185.     {
  186.     STRPTR name;
  187.     LONG i;
  188.  
  189.     for (i = 0; i < num; i++)
  190.     {
  191.         if ((name = text[(st + i)]) && MakeNode (list, name))
  192.         {
  193.         cnt++;
  194.         }
  195.         else
  196.         {
  197.         i = num;
  198.         }
  199.     }
  200.     }
  201.     return (cnt);
  202. }
  203.  
  204. /* All nodes on the list must have been allocated using AllocVec() */
  205. VOID
  206. FreeExecList (struct List * list, ULONG (*func) (), ULONG data,...)
  207. {
  208.     /* Make sure we have a list */
  209.     if (list)
  210.     {
  211.     /* Make sure there are entries in the list */
  212.     if (list->lh_TailPred != (struct Node *) list)
  213.     {
  214.         struct Node *node, *nxtnode;
  215.         ULONG cnt = 0L;
  216.  
  217.         /* Let's start at the very beginning... */
  218.         node = list->lh_Head;
  219.  
  220.         /* Continue while there are still nodes to remove */
  221.         while (nxtnode = node->ln_Succ)
  222.         {
  223.         /* Remove the node from the list */
  224.         Remove (node);
  225.  
  226.         /* Do we have a special removal hook? */
  227.         if (func)
  228.         {
  229.             /* Call the special removal routine, passing the node */
  230.             (*(func)) (node, cnt, (VOID *) & data);
  231.         }
  232.  
  233.         /* Free the node itself */
  234.         FreeVec ((APTR) node);
  235.  
  236.         /* Go on to the next node */
  237.         node = nxtnode;
  238.         cnt++;
  239.         }
  240.     }
  241.     }
  242. }
  243.  
  244. /* Perform an operation on all members of a list */
  245. VOID HandleList (struct List * list, VOID (*func)(struct Node *, ULONG, void *, ...), void *data,...)
  246. {
  247.     struct Node *node;
  248.     ULONG cnt;
  249.  
  250.     for (node = list->lh_Head, cnt = 0;
  251.      node->ln_Succ;
  252.      node = node->ln_Succ, cnt++)
  253.     {
  254.     (*(func))(node, cnt, (VOID *) &data);
  255.     }
  256. }
  257.  
  258. struct Node *FindSelected (struct List * list, WORD num)
  259. {
  260.     struct Node *node;
  261.  
  262.     for (node = list->lh_Head; node->ln_Succ; node = node->ln_Succ)
  263.     {
  264.     if (num-- == 0)
  265.     {
  266.         return (node);
  267.     }
  268.     }
  269.  
  270.     return (NULL);
  271. }
  272.  
  273. BOOL CopyImage (struct Image * src, struct Image * dst)
  274. {
  275.     BOOL retval = FALSE;
  276.  
  277.     if (src && dst)
  278.     {
  279.     int msize;
  280.  
  281.     /* Copy the Image structure */
  282.     CopyMem (src, dst, sizeof (struct Image));
  283.  
  284.     /* Don't go pointing off into nowhere land */
  285.     dst->NextImage = NULL;
  286.  
  287.     /* Compute the size of the image data needed */
  288.     msize = RASSIZE (src->Width, src->Height) * src->Depth;
  289.  
  290.     /* Allocate the image data */
  291.     if (dst->ImageData = (USHORT *)
  292.         AllocVec (msize, MEMF_CLEAR | MEMF_CHIP))
  293.     {
  294.         /* Copy image data */
  295.         CopyMem (src->ImageData, dst->ImageData, msize);
  296.  
  297.         /* Indicate success */
  298.         retval = TRUE;
  299.     }
  300.     }
  301.  
  302.     return (retval);
  303. }
  304.  
  305. VOID FreeImage (struct Image * im)
  306. {
  307.  
  308.     if (im && im->ImageData)
  309.     {
  310.     FreeVec (im->ImageData);
  311.     im->ImageData = NULL;
  312.     }
  313. }
  314.  
  315. /* Get the file requester set up properly.  Returns 0 if no file node
  316.  * present, returns > 0 if there is a file node.  */
  317. LONG FixFileAndPath (struct FileRequester *rf, STRPTR name)
  318. {
  319.     LONG count = 0L;
  320.  
  321.     if (rf)
  322.     {
  323.     if (name)
  324.     {
  325.         stcgfp (rf->rf_Dir, name);
  326.         count = (LONG) stcgfn (rf->rf_File, name);
  327.     }
  328.     else
  329.     {
  330.         WORD dlen = (strlen (rf->rf_Dir) - 1);
  331.  
  332.         if ((dlen > 0) && (rf->rf_Dir[dlen] == '/'))
  333.         {
  334.         rf->rf_Dir[dlen] = 0;
  335.         }
  336.     }
  337.     }
  338.  
  339.     return (count);
  340. }
  341.  
  342. /* Return the protection bits for a file */
  343. LONG GetProtection (STRPTR name)
  344. {
  345.     struct FileInfoBlock __aligned fib;
  346.     LONG ret = (-1L);
  347.     BPTR lock;
  348.  
  349.     /* Get a lock on the file */
  350.     if (lock = Lock (name, SHARED_LOCK))
  351.     {
  352.  
  353.     /* Examine the file */
  354.     if (Examine (lock, &fib))
  355.     {
  356.         /* Get the protection bits */
  357.         ret = fib.fib_Protection;
  358.     }
  359.  
  360.     /* Unlock the old lock */
  361.     UnLock (lock);
  362.     }
  363.  
  364.     return (ret);
  365. }
  366.